<template>
  <div :id="id" :style="{ height:height,width:width}" class="chart"></div>
</template>

<script>
export default {
  name: "comment",
  props: {
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "200px"
    },
    dataX: {
      type: Array,
      default: () => [120,200,150,80,70,110,130]
    },
    dataY: {
      type: Array,
      default: () => ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      let echarts = require('echarts');
      this.chart = echarts.init(document.getElementById(this.id));
      const option = {
        xAxis: {
          type: 'value'
        },
        yAxis: {
          type: 'category',
          data: this.dataY
        },
        grid: {
          left: 0,
          right: '2%',
          top: 0,
          bottom: 0,
          containLabel: true
        },
        series: [{
          data: this.dataX,
          type: 'bar'
        }]
      };
      this.chart.setOption(option);
    }
  }
}
</script>

<style scoped>
.chart {
  /*padding-left: 2rem;*/
}
</style>
